package com.junjiao.util.java.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadUtil {
	public static ThreadPoolExecutor taskPool;

	static {
		taskPool = new ThreadPoolExecutor(1, 10, 5, TimeUnit.SECONDS,
				new ArrayBlockingQueue<Runnable>(50),
				new ThreadPoolExecutor.CallerRunsPolicy());
	}

	/**
	 * 
	 * @param corePoolSize
	 *            池中所保存的线程数，包括空闲线程
	 * @param maximumPoolSize
	 *            池中允许的最大线程数
	 * @param keepAliveTime
	 *            当线程数大于核心时，此为终止前多余的空闲线程等待新任务的最长时间
	 * @param unit
	 *            参数的时间单位
	 * @param workQueue
	 *            执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务
	 * @param threadFactory
	 *            执行程序创建新线程时使用的工厂
	 * @param handler
	 *            由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序
	 */
	public ThreadUtil(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
			RejectedExecutionHandler handler) {
		taskPool = new ThreadPoolExecutor(1, 10, 5, TimeUnit.SECONDS,
				new ArrayBlockingQueue<Runnable>(50),
				new ThreadPoolExecutor.CallerRunsPolicy());
		new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
				unit, workQueue, handler);
	}
}
